﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RussiaLibrary
{
    public class LPolygon : Class_PolygonBase
    {
        public override bool Init(byte[,] Bytes_TetrisByte)
        {
            int_State = Class_Common.GetRandomInt() % 8;
            switch (int_State)
            {
                case 0://L  初始化L形方块
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(2, 5), new KeyValuePair<int, int>(2, 4),
                                                      new KeyValuePair<int, int>(1, 4), new KeyValuePair<int, int>(0, 4));
                case 1:
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(0, 6), new KeyValuePair<int, int>(1, 6),
                                                      new KeyValuePair<int, int>(1, 5), new KeyValuePair<int, int>(1, 4));
                case 2:
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(0, 4), new KeyValuePair<int, int>(0, 5),
                                                      new KeyValuePair<int, int>(1, 5), new KeyValuePair<int, int>(2, 5));
                case 3:
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(1, 4), new KeyValuePair<int, int>(0, 4),
                                                      new KeyValuePair<int, int>(0, 5), new KeyValuePair<int, int>(0, 6));
                case 4:
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(2, 4), new KeyValuePair<int, int>(2, 5),
                                                      new KeyValuePair<int, int>(1, 5), new KeyValuePair<int, int>(0, 5));
                case 5:
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(1, 6), new KeyValuePair<int, int>(0, 6),
                                                      new KeyValuePair<int, int>(0, 5), new KeyValuePair<int, int>(0, 4));
                case 6:
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(0, 5), new KeyValuePair<int, int>(0, 4),
                                                      new KeyValuePair<int, int>(1, 4), new KeyValuePair<int, int>(2, 4));
                case 7:
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(0, 4), new KeyValuePair<int, int>(1, 4),
                                                      new KeyValuePair<int, int>(1, 5), new KeyValuePair<int, int>(1, 6));
            }
            return false;
        }

        public override void Change(byte[,] Bytes_TetrisByte)
        {
            bool canChange = true;
            if (points[1].Key + 2 < Class_Common.Width && points[1].Key - 2 > 0 && points[1].Value + 2 < Class_Common.Length && points[1].Value - 2 > 0) //防止越界
            {
                for (int i = points[1].Key - 1; i < points[1].Key + 2; i++)//4x4的方格作为变形框，该方格里面有任何一处被占用，那么则不能变形
                {
                    for (int j = points[1].Value - 1; j <= points[1].Value + 2; j++)
                    {
                        if (Bytes_TetrisByte[i, j] == 2)
                        {
                            canChange = false;
                        }
                    }
                }
                canChange = false;
            }
            else
            {
                canChange = false;
            }

 //           if(canChange)
 //           {
            switch (int_State)//int_State
                {
                    case 0:
                        {

                            List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                            for (int i = 0; i < 4; i++)//i表示第几个点的处理
                            {
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                                KeyValuePair<int, int> tempPoint;
                                switch (i)
                                {
                                    case 0:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 1:
                                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                        newPoints.Add(points[i]);
                                        break;
                                    case 2:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value + 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 3:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 2, points[i].Value + 2);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    default:
                                        break;
                                }

                            }
                            int_State = 3;
                            points = newPoints;
                        }
                        break;
                    case 1:
                        {
                            KeyValuePair<int, int> tempPoint;
                            List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                            for (int i = 0; i < 4; i++)//i表示第几个点的处理
                            {
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                                switch (i)
                                {
                                    case 0:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value + 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 1:
                                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                        newPoints.Add(points[i]);
                                        break;
                                    case 2:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value + 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 3:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 2, points[i].Value + 2);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    default:
                                        break;
                                }

                            }
                            int_State = 0;
                            points = newPoints;
                        }//
                        break;
                    case 2:
                        {
                            KeyValuePair<int, int> tempPoint;
                            List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                            for (int i = 0; i < 4; i++)//i表示第几个点的处理
                            {
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                                switch (i)
                                {
                                    case 0:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value + 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 1:
                                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                        newPoints.Add(points[i]);
                                        break;
                                    case 2:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 3:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 2, points[i].Value - 2);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    default:
                                        break;
                                }

                            }//
                            int_State = 1;
                            points = newPoints;
                        }
                        break;
                    case 3:
                        {
                            KeyValuePair<int, int> tempPoint;
                            List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                            for (int i = 0; i < 4; i++)//i表示第几个点的处理
                            {
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                                switch (i)
                                {
                                    case 0:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 1:
                                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                        newPoints.Add(points[i]);
                                        break;
                                    case 2:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 3:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 2, points[i].Value - 2);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    default:
                                        break;
                                }

                            }
                            int_State = 2;
                            points = newPoints;
                        }//
                        break;
                    case 4:
                        {
                            KeyValuePair<int, int> tempPoint;
                            List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                            for (int i = 0; i < 4; i++)//i表示第几个点的处理
                            {
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                                switch (i)
                                {
                                    case 0:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value + 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 1:
                                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                        newPoints.Add(points[i]);
                                        break;
                                    case 2:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value + 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 3:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 2, points[i].Value + 2);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    default:
                                        break;
                                }

                            }
                            int_State = 7;
                            points = newPoints;
                        }//
                        break;
                    case 5:
                        {
                            KeyValuePair<int, int> tempPoint;
                            List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                            for (int i = 0; i < 4; i++)//i表示第几个点的处理
                            {
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                                switch (i)
                                {
                                    case 0:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 1:
                                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                        newPoints.Add(points[i]);
                                        break;
                                    case 2:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value + 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 3:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 2, points[i].Value + 2);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    default:
                                        break;
                                }//

                            }
                            int_State = 4;
                            points = newPoints;
                        }
                        break;
                    case 6:
                        {
                            KeyValuePair<int, int> tempPoint;
                            List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                            for (int i = 0; i < 4; i++)//i表示第几个点的处理
                            {
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                                switch (i)
                                {
                                    case 0:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 1:
                                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                        newPoints.Add(points[i]);
                                        break;
                                    case 2:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 3:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 2, points[i].Value - 2);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    default:
                                        break;
                                }
                            }
                            int_State = 5;
                            points = newPoints;
                        }
                        break;
                    case 7:
                        {
                            KeyValuePair<int, int> tempPoint;
                            List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                            for (int i = 0; i < 4; i++)//i表示第几个点的处理
                            {
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                                switch (i)
                                {
                                    case 0:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value + 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 1:
                                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                        newPoints.Add(points[i]);
                                        break;
                                    case 2:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 3:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 2, points[i].Value - 2);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    default:
                                        break;
                                }

                            }
                            int_State = 6;
                            points = newPoints;
                        }
                        break;
                    default:
                        break;
                }
//            }


        }
    }
}
